package com.karl.loadingviews;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.karl.loadingviews.adapter.ItemAdapter;
import com.karl.loadingviews.headers.AliPayHeaderView;
import com.karl.loadingviews.headers.ITHomeHeaderView;
import com.karl.loadingviews.headers.IndicatorHeaderView;
import com.karl.loadingviews.headers.MiHeaderView;
import com.lcodecore.tkrefreshlayout.IHeaderView;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.rf_loading)
    TwinklingRefreshLayout mRefresh;
    @BindView(R.id.rv_loading)
    RecyclerView recyclerView;

    private ItemAdapter itemAdapter;

    private List<String> mItems;
    private List<IHeaderView> mHeaders;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initVariable();
        initEvents();
    }

    private void initVariable(){
        mItems = new ArrayList<>();
        mHeaders = new ArrayList<>();

        ITHomeHeaderView homeHeaderView = new ITHomeHeaderView(this);
        mHeaders.add(homeHeaderView);
        mItems.add("LoadingViews: 仿IT之家下拉控件");

        IndicatorHeaderView indicatorHeaderView = new IndicatorHeaderView(this);
        mHeaders.add(indicatorHeaderView);
        mItems.add("LoadingViews: 仿ios邮件下拉控件");

        AliPayHeaderView aliPayHeaderView = new AliPayHeaderView(this);
        mHeaders.add(aliPayHeaderView);
        mItems.add("LoadingViews : 仿阿里Pay下拉控件");

        MiHeaderView miHeaderView = new MiHeaderView(this);
        mHeaders.add(miHeaderView);
        mItems.add("LoadingViews : 仿小密圈下拉控件");

    }

    private void initEvents(){
        recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));

        if (itemAdapter==null){
            itemAdapter = new ItemAdapter(R.layout.item_loading,mItems);
        }

        recyclerView.setAdapter(itemAdapter);

        mRefresh.setHeaderView(mHeaders.get(0));

        itemAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //切换下拉控件
                if (position < mHeaders.size()){
                    mRefresh.setHeaderView(mHeaders.get(position));
                    mRefresh.startRefresh();
                }
            }
        });

        mRefresh.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mRefresh.finishRefreshing();
                    }
                },3000);
            }
        });

    }
}
